<?php

namespace App\V3\V3_2_7\Controllers;

use AlibabaCloud\SDK\Alinlp\V20200629\Alinlp;
use AlibabaCloud\SDK\Alinlp\V20200629\Models\GetNerChEcomRequest;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use App\Http\Requests\V3_2_7\AddBillsRequest;
use App\Http\Requests\V3_2_7\MessBoardRequest;
use App\Http\Requests\V3_2_7\MessBoardRequiredRequest;
use App\V3\V3_2_7\Services\MessBoardService;
use Darabonba\OpenApi\Models\Config;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\JsonResponse;

class MessBoardController extends BaseController
{
    /**
     *提交留言板内容
     * Author: zlf
     * Created at 2024/7/10 17:27
     * @param Request $request
     * @return JsonResponse
     */
    public function addMessBoardContent(Request $request): JsonResponse
    {
        $data = $request->all();
        if (empty($data['content'])) {
            return fail('', '请填写留言内容！');
        }
        $data['time'] = time();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->operateLog('添加留言板内容', $data, $data['time']);

        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return response()->json(MessBoardService::addMessBoardContent($data));
            });
    }

    /**
     *内容列表
     * Author: zlf
     * Created at 2024/7/10 17:16
     * @param MessBoardRequest $request
     * @return JsonResponse
     */
    public function messBoardContentList(MessBoardRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = !empty($data['user_id']) ? $data['user_id'] : $this->getUserId();
        return $this->success(MessBoardService::messBoardContentList($data));
    }

    /**
     *选择账号
     * Author: zlf
     * Created at 2024/7/11 9:21
     * @param Request $request
     * @return JsonResponse
     */
    public function chooseUserAccount(Request $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        return $this->success(MessBoardService::selUserList($data));
    }

    /**
     *查询商品列表
     * Author: zlf
     * Created at 2024/7/11 15:31
     * @param MessBoardRequiredRequest $request
     * @return JsonResponse
     */
    public function selGoodsList(MessBoardRequiredRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        return $this->success(MessBoardService::selGoodsList($data));
    }

    /**
     *创建单据
     * Author: zlf
     * Created at 2024/7/11 15:31
     * @param AddBillsRequest $request
     * @return JsonResponse
     */
    public function addBills(AddBillsRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['time'] = time();
        $data['batch_title'] = $this->batchTitle();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $data['isUseShelf'] = $this->isUseShelf();
        $data['isShareShelfLocation'] = $this->isShareShelfLocation();
        return response()->json(MessBoardService::addBills($data));
    }

    /**
     *留言新消息
     * Author: zlf
     * Created at 2024/7/15 10:19
     * @param Request $request
     * @return JsonResponse
     */
    public function isNewBoardContent(Request $request): JsonResponse
    {
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        return success(MessBoardService::isNewBoardContent($data));
    }

    /**
     *关键词分词
     * Author: zlf
     * Created at 2024/7/13 15:01
     * @param Request $request
     * @return JsonResponse
     * @throws \JsonException
     */
    public function splitKeywordList(Request $request): ?JsonResponse
    {
        $config = new Config();
        $config->accessKeyId = 'LTAI5tDor4tV4pJtPRhsvMRH';
        $config->accessKeySecret = 'cu2XEQlN3GVLKzHB7h8pLo6SflNMgr';
        $config->regionId = "cn-hangzhou";
        $config->endpoint = "alinlp.cn-hangzhou.aliyuncs.com";
        $client = new Alinlp($config);
        $req = new GetNerChEcomRequest();
        $req->serviceCode = 'alinlp';
        $req->text = $request->keyword;
        $wordsArr = [];
        try {
            $response = $client->getNerChEcom($req);
            //$json = json_encode($response->body, JSON_THROW_ON_ERROR);
            if (!empty($response->body->data)) {
                $arr = json_decode($response->body->data, true, 512, JSON_THROW_ON_ERROR);
                if ($arr['success'] === true) {
                    foreach ($arr['result'] as $rVal) {
                        $wordsArr[] = $rVal['word'];
                    }
                }
            }
            return success($wordsArr);
        } catch (TeaUnableRetryError $e) {
            return fail('', $e->getMessage());
        }

    }
}
